package com.team.mp.api.service.impl;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team.mp.api.bo.ResidentBasicInfoBo;
import com.team.mp.api.service.intf.ResidentDetailService;
import com.team.mp.mapper.dao.LabelDao;
import com.team.mp.mapper.dao.ResidentDetailDao;
import com.team.mp.mapper.dao.ResidentTagDao;
import com.team.mp.mapper.dao.SignRecordDao;
import com.team.mp.mapper.dto.ResidentDto;
import com.team.mp.mapper.entity.Label;
import com.team.mp.mapper.entity.ResidentDetail;
import com.team.mp.mapper.entity.ResidentTag;
import com.team.mp.vo.R;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @program: TProject_Mapper
 * @description: 业务接口实现层
 * @author: aming
 * @create: 2023-03-27 17:52:13
 */
@Service
public class ResidentDetailServiceImpl implements ResidentDetailService {
    /**
     * 注入持久层对象
     */
    @Resource
    private ResidentDetailDao dao;
    @Resource
    private LabelDao dao1;
    @Resource
    private ResidentTagDao dao2;
    @Resource
    private SignRecordDao dao3;

    /**
     * 新增内容
     */
    @Override
    @Transactional
    public R saveBasic(ResidentBasicInfoBo bo) {
        if (bo != null) {
            ResidentDetail residentDetail = new ResidentDetail();
            BeanUtils.copyProperties(bo, residentDetail);
            int i = dao.insert(residentDetail);
            int j = 0;
            List<String> lables = bo.getLables();
            if (lables != null && lables.size() > 0) {
                for (String label : lables) {
                    QueryWrapper<Label> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("name", label);
                    Label label1 = dao1.selectOne(queryWrapper);
                    if (Objects.nonNull(label1)) {
                        ResidentTag residentTag = new ResidentTag(residentDetail.getId(), label1.getId());
                        j = dao2.insert(residentTag);
                    }
                }
            }
            if (i > 0 & j > 0) {
                return R.ok();
            }
        }
        return R.fail("亲，新增异常！");
    }

    /**
     * 修改内容
     */
    @Override
    public R update(ResidentDetail residentDetail) {
        if (residentDetail != null) {
            int i = dao.updateById(residentDetail);
            if (i > 0) {
                return R.ok();
            }
        }
        return R.fail("亲，修改异常！");
    }

    /**
     * 删除
     */
    @Override
    public R del(long id) {
        if (id > 0) {
            int i = dao.deleteById(id);
            if (i > 0) {
                return R.ok();
            }
        }
        return R.fail("删除异常");
    }

    /**
     * 查询详情
     */
    @Override
    public R queryById(long id) {
        return R.ok(dao.selectById(id));
    }

    /**
     * 查询 所有数据
     */
    @Override
    public R queryAll() {
        return R.ok(dao.selectList(null));
    }

    @Override
    public R queryPage(int page,
                       int size,
                       String state,
                       String deptname,
                       String teamname,
                       String packname) {
        IPage<ResidentDto> pageInfo = dao.queryPage(new Page<>(page, size),
                StringUtils.hasLength(state) ? Integer.parseInt(state) : null,
                deptname,
                teamname,
                packname);
        return R.ok(pageInfo);
    }

}
